<?php
/**
 * Command class, used to keep commands data
 * @package action
 * @author alcaitiff
 */
class Command
{
	/**
     *
     * @var Char
     */
    private $character;
    private	$timestamp,	$command,$parameters=array(),$executionTime;
	/**
     *
     * @param string $characterId Character name
     * @param int $ts Time
     * @param string $command Command line
     * @param array $parameters Parameters
     * @param int $executionTime Time to execute
     */
	function __construct($characterId,$ts,$command,$parameters,$executionTime){
		$this->character=new Char($characterId);
		$this->timestamp=$ts;
		$this->command=$command;
		$this->parameters=explode(' ',$parameters);
		$this->executionTime=$executionTime;
	}
	/**
     * Magic get
     * @param string $property Property name
     * @return Character|int|Array|string
     */
	public function __get($property){
		return $this->$property;
	}
    /**
     * Arrange parameters removing null and empty values and returning an array with omited values filled
     * @param array $parameters
     * @param int $min
     * @param int $max
     * @param bool $qdt
     * @return array
     */
    public function getArrangedParameters($min=0,$max=5,$qtd=false){
        //Clean null values
        $parameters=Utils::removeArrayNullValues($this->parameters,FALSE);

        $parsNum=count($parameters);

        if( $parsNum<$min || $parsNum>$max) {
			return null;
		}

                /**
         *  Number = (integer >0)
         *  Item = string
         *  Conatiner = string
         *
         *  Parameters:
         *
         * 1: str                        all    elemnt 1;
         * 2: num, str          		 n      elemnt 1;
         * 2: "all", str       			 all    elemnt 1;
         * 2: str, num        			 all    elemnt n;
         * 2: str, str        			 all    elemnt 1 from conatiner 1;
         * 3: "all", str, str            all    elemnt 1 from container 1;
         * 3: "all", str, num            all    elemnt n;
         * 3: num, str, num              n      elemnt n;
         * 3: num, str, str              n      elemnt 1 from container 1;
         * 3: str, str, num              all    elemnt 1 from container n;
         * 3: str, num, str              all    elemnt n from container 1;
         * 4: "all", str,  str, num      all    elemnt 1 from container n;
         * 4: "all", str,  num, str      all    elemnt n from container 1;
         * 4: num, str, str, num         n      elemnt 1 from container n;
         * 4: num, str, num, str         n      elemnt n from container 1;
         * 4: str,  num, str, num        all      elemnt n from container n;
         * 5: num, str, num, str, num	 n      elemnt n from container n;
         * 5: "all", str, num, str, num	 all    elemnt n from container n;
         *
         */


        switch($parsNum){
            case 5:
                if( $qtd &&
                    ($parameters[0]=='all' || Utils::isIntegerGreaterThanZero($parameters[0])) &&
                    Utils::isIntegerGreaterThanZero($parameters[2]) &&
                    Utils::isIntegerGreaterThanZero($parameters[4])){
                    //Parameters OK 'all'|int str int str int
                    return array($parameters[0], $parameters[1], $parameters[2], $parameters[3], $parameters[4]);
                }else{
                    return null;
                }
                break;
            case 4:
                if($qtd && $parameters[0]=='all' && Utils::isIntegerGreaterThanZero($parameters[3])){
                    //Parameters OK 'all' str str int
                    return array('all', $parameters[1], 1, $parameters[2], $parameters[3]);
                }elseif($qtd && $parameters[0]=='all' && Utils::isIntegerGreaterThanZero($parameters[2])){
                    //Parameters OK 'all' str int str
                    return array('all', $parameters[1], $parameters[2], $parameters[3],1);
                }elseif($qtd &&
                        Utils::isIntegerGreaterThanZero($parameters[0]) &&
                        Utils::isIntegerGreaterThanZero($parameters[3])){
                    //Parameters OK int str str int
                    return array($parameters[0], $parameters[1], 1, $parameters[2], $parameters[3]);
                }elseif($qtd &&
                        Utils::isIntegerGreaterThanZero($parameters[0]) &&
                        Utils::isIntegerGreaterThanZero($parameters[2])){
                    //Parameters OK int str int str
                    return array($parameters[0], $parameters[1], $parameters[2], $parameters[3], 1);
                }elseif(Utils::isIntegerGreaterThanZero($parameters[1]) &&
                        Utils::isIntegerGreaterThanZero($parameters[3])){
                    //Parameters OK str int str int
                    return array('all',$parameters[0], $parameters[1], $parameters[2], $parameters[3]);
                }else{
                    return null;
                }
                break;

            case 3:
                if($qtd && $parameters[0]=='all' && Utils::isIntegerGreaterThanZero($parameters[2])){
                    //Parameters OK 'all' str int
                    return array('all', $parameters[1], $parameters[2]);
                }elseif($qtd && $parameters[0]=='all'){
                    //Parameters OK 'all' str str
                    return array('all', $parameters[1], 1, $parameters[2],1);
                }elseif($qtd &&
                        Utils::isIntegerGreaterThanZero($parameters[0]) &&
                        Utils::isIntegerGreaterThanZero($parameters[2])){
                    //Parameters OK int str int
                    return array($parameters[0], $parameters[1], $parameters[2]);
                }elseif($qtd &&
                        Utils::isIntegerGreaterThanZero($parameters[0])){
                    //Parameters OK int str str
                    return array($parameters[0], $parameters[1], 1, $parameters[2], 1);
                }elseif(Utils::isIntegerGreaterThanZero($parameters[1])){
                    //Parameters OK str int str
                    return array('all',$parameters[0], $parameters[1], $parameters[2], 1);
                }elseif(Utils::isIntegerGreaterThanZero($parameters[2])){
                    //Parameters OK str str int
                    return array('all',$parameters[0], 1, $parameters[1], $parameters[2]);
                }else{
                    return null;
                }
                break;
            case 2:
                if($qtd && Utils::isIntegerGreaterThanZero($parameters[0])){
                    //Parameters OK int str
                    return array($parameters[0], $parameters[1],1);
                }elseif($qtd && $parameters[0]=='all'){
                    //Parameters OK all str
                    return array('all',$parameters[1],1);
                }elseif(Utils::isIntegerGreaterThanZero($parameters[1])){
                    //Parameters OK str, int
                    return array('all',$parameters[0],$parameters[1]);
                }else{
                    //Parameters OK str str
                    return array('all',$parameters[0],1,$parameters[1],1);
                }
                break;
            case 1:
                //Parameters OK str
                return array('all',$parameters[0],1);
                break;
            case 0:
                //No parameters
                return array();
                break;
            default:
                return null;
        }
    }
}